package com.enqualcomm.kids.mvp.question;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.InputFilter;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.config.Constants;
import com.enqualcomm.kids.mvp.terminalinfo.TerminalInfoCallBack;
import com.enqualcomm.kids.view.MyInputFilter;

import common.utils.DensityUtil;
import common.utils.PromptUtil;

/**
 * Created by Administrator on 2016/3/14.
 */
public class MySetRewardDialog extends Dialog {
    public MySetRewardDialog(Context context, String content, TerminalInfoCallBack callBack) {
        super(context);
        this.context = context;
        this.callBack = callBack;
        this.content = content;
    }

    private int type = -1;
    private TerminalInfoCallBack callBack;
    private Context context;
    private EditText myEdit;
    private String content;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_myedit);

        setDialogAttributes();
        initDialogView();
    }

    public void setDialogAttributes() {
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();

        DisplayMetrics dm = new DisplayMetrics();
        dialogWindow.getWindowManager().getDefaultDisplay().getMetrics(dm);

        lp.width = dm.widthPixels - DensityUtil.dip2px(getContext(), 30); // 宽度
        // lp.alpha = 0.9f; // 透明度

        dialogWindow.setAttributes(lp);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    public void initDialogView() {
        //1.初始化标题
        TextView titleText = (TextView) findViewById(R.id.titleText);
        myEdit = (EditText) findViewById(R.id.myEdit);

//        //2.初始化编辑框
//        String str = tv.getText().toString();
        titleText.setText(context.getString(R.string.set_reward_title));
//        if (content.length() != 0) {  //说了，可以设置为空，让你为空你就为空，不要说那么多
            myEdit.setText(content);
            myEdit.setSelection(content.length());
//        }
//        myEdit.setFilters(new InputFilter[]{new MyInputFilter()});


        //3.初始化确定，取消按钮
        findViewById(R.id.cancelBtn).setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View arg0, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    //隐藏输入法
//                    inputMethodManager.hideSoftInputFromWindow(myEdit.getWindowToken(), 0);
                    dismiss();
                }
                return false;
            }
        });

        findViewById(R.id.sureBtn).setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    String str = myEdit.getText().toString().trim();
                    if (/*!"".equals(str)*/str.length()<11) {
                        callBack.inputOver(str);
                        dismiss();
                    } else {
                        PromptUtil.toast(getContext(), R.string.input_too_long);
                    }
                }
                return false;
            }
        });

    }
}
